#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import unittest
import json
from bzjsons import Jsoner
from json.decoder import JSONDecodeError

__project_ = 'bzjsons'
__file_name__ = 'loads_test'
__author__ = 'bright.zhang'
__time__ = '2020/6/23 9:49'


# for testing purpose.
class DemoClass:
    def __init__(self, id_=None, name_=None, enabled_=None, on_=None):
        self.id_ = id_
        self.name_ = name_
        self.enabled_ = enabled_
        self.on_ = on_


class BzjsonsLoadsTest(unittest.TestCase):
    def test_case_001__loads_basic_type_int(self):
        json1 = '100'
        des0 = json.loads(json1)
        des1 = Jsoner.loads(json1)
        des2 = Jsoner.loads_as_instance(json1)
        self.assertEqual(des0, des1, "different with json.loads().")
        self.assertEqual(100, des1, "incorrect")
        self.assertEqual(des0, des2, "different with json.loads().")
        self.assertEqual(100, des2, "incorrect")

    def test_case_002__loads_basic_type_float(self):
        json1 = '123.456'
        des1 = Jsoner.loads_as_instance(json1)
        des2 = json.loads(json1)
        self.assertEqual(des2, des1, "different with json.loads().")
        self.assertEqual(123.456, des1, "incorrect")

    def test_case_003__loads_basic_type_str(self):
        json1 = 'this is a json utility.'
        self.assertRaises(JSONDecodeError, json.loads, json1)
        self.assertRaises(JSONDecodeError, Jsoner.loads, json1)

    def test_case_004__loads_basic_type_bool(self):
        json1 = 'true'
        json2 = 'false'
        des11 = Jsoner.loads_as_instance(json1)
        des10 = json.loads(json1)
        des21 = Jsoner.loads_as_instance(json2)
        des20 = json.loads(json2)
        self.assertEqual(des10, des11, "different with json.loads().")
        self.assertTrue(des11, 'incorrect')
        self.assertEqual(des20, des21, "different with json.loads().")
        self.assertFalse(des21, 'incorrect')

    def test_case_005__loads_basic_type_null(self):
        json1 = 'null'
        des0 = json.loads(json1)
        des1 = Jsoner.loads_as_instance(json1)
        self.assertIsNone(des1, "it is not None, so incorrect.")
        self.assertIsNone(des0, "it is not None, so incorrect.")

    def test_case_006__loads_empty_list(self):
        json1 = '[]'
        des0 = json.loads(json1)
        print('des0:', des0)
        des1 = Jsoner.loads_as_instance(json1)
        self.assertEqual(0, len(des1), 'incorrect count.')
        des1_2 = Jsoner.loads_as_instance(json1, cls=DemoClass)
        self.assertEqual(0, len(des1_2), 'incorrect count.')

    def test_case_007__loads__null_object_list(self):
        json2 = '[{\"id_\": null, \"name_\": null, \"enabled_\": null, \"on_\": null}]'
        des2 = Jsoner.loads_as_instance(json2, cls=DemoClass)
        self.assertEqual(1, len(des2), 'incorrect count.')
        self.assertIsNone(des2[0].id_, 'incorrect id_')
        self.assertIsNone(des2[0].name_, 'incorrect name_')
        self.assertIsNone(des2[0].enabled_, 'incorrect enabled_')
        self.assertIsNone(des2[0].on_, 'incorrect on_')

    def test_case_008__loads__simple_object(self):
        json1 = '{\"id_\": 1001, \"name_\": \"this is a demo\", \"enabled_\": true, \"on_\": \"2020-02-02 02:02:02\"}'
        des1 = Jsoner.loads_as_instance(json1, cls=DemoClass)
        self.assertIsNotNone(des1, 'none got.')
        self.assertEqual(1001, des1.id_, 'incorrect - id_')
        self.assertEqual('this is a demo', des1.name_, 'incorrect - name_')
        self.assertEqual(True, des1.enabled_, 'incorrect - enabled_')
        self.assertEqual('2020-02-02 02:02:02', des1.on_.strftime('%Y-%m-%d %H:%M:%S'), 'incorrect - on_')

